package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.common.dto.ResDevice;
import com.bjb.vr.common.dto.ResDeviceContent;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.service.ResDeviceContentService;
import com.bjb.vr.common.service.ResDeviceService;
import com.bjb.vr.common.utils.DateTimeUtil;
import com.bjb.vr.common.utils.RedisUtil;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.redis.constants.RedisConstants;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.github.yitter.idgen.YitIdHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/3/29
 */
@Component
@Slf4j
public class CvrUpCourseListMessageHandler implements MessageHandler {

    @Autowired
    ResDeviceContentService resDeviceContentService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ResDeviceService resDeviceService;

    final String  LOCK_KEY = "res_device_content_course_id";

    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {

        String dbLockKey = RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY + LOCK_KEY+ rabbitWebSocketMsg.getDeviceId();

        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_UP_COURSE_LIST)){
            log.info("处理安装课程上传信息");
            //WebSocketMsgHandler.sendAdmin(rabbitWebSocketMsg);
            JSONObject _jsonObject = JSONObject.parseObject(message.getCommand().getParameter());
            JSONArray courseList = _jsonObject.getJSONArray("courseList");

            BaseResult<ResDevice> result = resDeviceService.getResDeviceByUdid(message.getCommand().getUdid());
            if(!result.isSuccess()){
                log.error("设备唯一标志符 "  + message.getCommand().getUdid()  + " " +  result.getMessage());
                return;
            }
            ResDevice resDevice = result.getData();
            List<ResDeviceContent>  resDeviceContentList = new ArrayList<>();
            if(courseList!=null && courseList.size()>0){
                Date curDate = new Date();
                for(int i=0;i<courseList.size();i++){
                    ResDeviceContent resDeviceContent = new ResDeviceContent();
                    Long contentId = Long.valueOf(courseList.get(i).toString());
                    resDeviceContent.setUdid(message.getCommand().getUdid());
                    resDeviceContent.setContentId(contentId);
                    resDeviceContent.setTenantId(resDevice.getTenantId());
                    resDeviceContent.setGmtCreate(DateTimeUtil.getLocalDateTime(curDate));
                    resDeviceContent.setGmtModified(DateTimeUtil.getLocalDateTime(curDate));
                    resDeviceContent.setDataId(YitIdHelper.nextId());
                    resDeviceContentList.add(resDeviceContent);
                }


            }
            String value = String.valueOf(System.currentTimeMillis());
            if(redisUtil.lock(dbLockKey,value,RedisConstants.WEBSOCKET_DB_OP_LOCK_KEY_EXPIRE_TIME)) {
                resDeviceContentService.deleteResDeviceSetupCourseByUdid(rabbitWebSocketMsg.getDeviceId());
                if(resDeviceContentList.size()>0) {
                    resDeviceContentService.saveBatchResDeviceSetupCourseList(resDeviceContentList);
                }
                redisUtil.unlock(dbLockKey, value);
            }


        }
    }
}
